package controller;

import dao.NurseDao;
import dao.NurseRoleDao;
import dao.PositionDao;
import entity.Nurse;
import entity.Position;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import service.NurseService;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/Nurse")
@Scope("prototype")
public class NurseController {

    @Autowired
    private NurseDao nurseDao;
    @Autowired
    private NurseService nurseService;
    @Autowired
    private PositionDao positionDao;
    @Autowired
    private NurseRoleDao nurseRoleDao;

    @RequestMapping("/query")
    @ResponseBody
    public Map<String,Object> query(Nurse nurse, @RequestParam(value = "pageNumber",defaultValue = "1",required = false) int pageNumber, @RequestParam(value = "pageSize",defaultValue = "5",required = false) int pageSize){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("total",nurseDao.getCount(nurse));
        map.put("rows",nurseService.query(nurse,pageSize,pageNumber));
        return map;
    }
    @RequestMapping("/del")
    @ResponseBody
    public boolean del(int nId){
        boolean flag = nurseDao.del(nId);
        return flag;
    }
    @RequestMapping("/add")
    public String add(Nurse nurse,@RequestParam(value = "file")MultipartFile file,HttpServletRequest request)throws Exception{
        System.out.println(nurse.getnName()+"----");
        System.out.println(nurse.getPosId()+"----");
        System.out.println(nurse.getnEmail()+"----");
        String pathRoot = request.getSession().getServletContext().getRealPath("");
        String path="";
        if((!file.isEmpty())){
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            String contentType=file.getContentType();
            String imageName=contentType.substring(contentType.indexOf("/")+1);
            path="/statics/images/"+uuid+"."+imageName;
            file.transferTo(new File(pathRoot+path));
        }
        nurse.setnImg(path);
        nurse.setdDepartmentId(nurse.getPosId());
        if(nurseDao.add(nurse)){
            long posId = nurse.getPosId();
            long nId = nurseDao.getNid();
            System.out.println(nId+"----");
            nurseRoleDao.add(posId,nId);
            return "redirect:Nurse.jsp";
        }else{
            return "redirect:AddNurse.jsp";
        }

    }



    @RequestMapping("/distributionRole")
    @ResponseBody
    public  Map<String,Object> distributionRole(HttpServletRequest request, @RequestParam(value = "pageNumber",defaultValue = "1",required = false) int pageNumber, @RequestParam(value = "pageSize",defaultValue = "5",required = false) int pageSize){
        Map<String,Object> map = new HashMap<String,Object>();
        int offset = (pageNumber-1)*pageSize;
        int limit = pageSize;
        map.put("rows",nurseDao.distributionRole(offset,limit));
        map.put("total",nurseDao.getNumber());
        return map;
    }

    // 获取角色
    @RequestMapping("/queryPosition")
    @ResponseBody
    public List<Position> queryPosition(){
        return positionDao.query();
    }

    // 分配角色
    @RequestMapping("/updateRole")
    public String updateRole(long nId,long posId){
        if(nurseDao.updateRole(nId,posId)){
            nurseRoleDao.upd(posId,nId);
        }
        return "redirect:distributionRole.jsp";
    }
    // 查询手机号
    @RequestMapping("/queryCall")
    @ResponseBody
    public List<Nurse> queryCall(){
        return nurseDao.list();
    }



}
